﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Xml;
using System.Web.UI;
using System.Text;


namespace Sever.Controllers
{
    public class HomeController : Controller
    {
        
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file,string a1,string a2)
        {
            string Name = a1;
            string pass = a2;
            bool ok = false;

            string rootdata =Server.MapPath("~/App_Data");
            try
            {
                
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Images"), fileName);
                    file.SaveAs(path);
                }
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/Database.XML");
                try
                {
                    XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + Name + "']");
                    if (node.Count == 0)
                    {
                        XmlNode nodeLH = Doc.DocumentElement;
                        XmlNode nodeSV = Doc.CreateElement("User");

                        XmlAttribute HT = Doc.CreateAttribute("UserName");
                        HT.Value = Name;
                        XmlAttribute Pa = Doc.CreateAttribute("Pass");
                        Pa.Value = pass;
                        XmlAttribute Av = Doc.CreateAttribute("Avatar");
                        Av.Value = file.FileName;
                        XmlAttribute stt = Doc.CreateAttribute("status");
                        stt.Value = "offline";

                        nodeSV.Attributes.Append(HT);
                        nodeSV.Attributes.Append(Pa);
                        nodeSV.Attributes.Append(Av);
                        nodeSV.Attributes.Append(stt);

                        nodeLH.AppendChild(nodeSV);

                        Doc.Save(rootdata + "/Database.XML");
                        ok = true;
                    }
                    else
                    {
                        ok=false;
                    }
                }
                catch (Exception ex)
                {
                    ok = false;
                }
                if (ok == false)
                {
                    string script = "<script type=\"text/javascript\">alert('Please Try Again ...');</script>";
                }
                else
                {
                    string script = "<script type=\"text/javascript\">alert('Successful ...');</script>";
                    Response.Redirect("http://localhost:18791/Default.aspx");
                    
                }
                
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
        
    }
}
